package com.core.sgip.httpclient;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.core.sgip.common.SGIPproperties;
import com.core.sgip.httpclient.util.MD5Tools;
import com.core.sgip.httpclient.util.ToolSecuritySHA;
import com.google.gson.Gson;

@SuppressWarnings("deprecation")
public class ServiceDoHandle {

	private static Logger logger = Logger.getLogger(ServiceDoHandle.class);
	public static ServiceDoHandle service = new ServiceDoHandle();

	public static final String url = SGIPproperties.HTTPURL;
//	public static final String appkey = SGIPproperties.HTTPAPPKEY;
//	public static final String secretKey = SGIPproperties.HTTPSECRETKEY;

	public static void main(String[] args) {
//		order("cxinfox","abcdefg","13246864965", "【深圳联通】短信测试,退定回TD", "guotest");
		if ("8613246864965".startsWith("86")) {
			System.out.println( "8613246864965".substring(2, 12));
		}
	}
	
	@SuppressWarnings({ "resource" })
//	public static String order(String phoneNo, String content, String customerId) {
		public static String order(String appkey,String secretKey,String phoneNo, String content, String customerId) {
		logger.info("ServiceDoHandle order param{} appkey= "+appkey+",secretKey = "+secretKey+",phoneNo = "+phoneNo+",content = "+content+",customerId = "+customerId);
		if (phoneNo.startsWith("86")) {
			phoneNo = phoneNo.substring(2, 13);
		}
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("appkey", appkey.trim());
		params.put("phoneNo", phoneNo.trim());
		try {
			params.put("content", URLEncoder.encode(content, "UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		params.put("customerId", customerId.trim());

		String paramsSort = ToolSecuritySHA.dataDecrypt(params);
		String sig = MD5Tools.MD5(paramsSort + secretKey);

		params.put("sig", sig.toUpperCase());

		String json = new Gson().toJson(params);

		logger.info("sgip 请求业务处理参数:" + json);
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost method = new HttpPost(url);
		String resultJson = "";
		try {
			if (null != json) {
				// 解决中文乱码问题
				StringEntity entity = new StringEntity(json, "utf-8");
				entity.setContentEncoding("utf-8");
				entity.setContentType("application/json");
				method.setEntity(entity);
			}
			HttpResponse result = httpClient.execute(method);
			if (result.getStatusLine().getStatusCode() == 200) {
				resultJson = EntityUtils.toString(result.getEntity(), "utf-8");
				logger.info("sgip 请求业务处理响应: " + resultJson);
				
			}
			if(resultJson ==null ||"".equals(resultJson)){
				resultJson ="{\"code\": \"0001\",\"msg\":\"请求http异常\"}";
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("sgip 请求业务处理异常: " + e);
		}
		return resultJson;

	}

}
